package dwetl.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

public class DimTable implements Serializable {
	private static final long serialVersionUID = 4736665677339126463L;
	private String table_name = null;
	private String table_alias = null;
	private List<DimColumn> etl_columns = new ArrayList<DimColumn>();

	private SortedSet<String> unique_key_columns=null;
	private SortedSet<DimColumn> unique_key_column_set=null;
	private List<String> uncheck_columns=null;
	private String unique_key_date_column=null;
	private boolean if_exists_skip=true;

	public DimTable() {
		super();
		// TODO Auto-generated constructor stub
	}

	public SortedSet<String> getUnique_key_columns() {
		return unique_key_columns;
	}

	public void setUnique_key_columns(SortedSet<String> unique_key_columns) {
		this.unique_key_columns = unique_key_columns;
		unique_key_column_set=new TreeSet<>();
		for(String cn:unique_key_columns){
			unique_key_column_set.add(this.findDimColumnByCol(cn));
		}
	}

	public SortedSet<DimColumn> getUnique_key_column_set(){
		return this.unique_key_column_set;
	}

	public List<String> getUncheck_columns() {
		return uncheck_columns;
	}

	public void setUncheck_columns(List<String> uncheck_columns) {
		this.uncheck_columns = uncheck_columns;
	}

	public String getUnique_key_date_column() {
		return unique_key_date_column;
	}

	public void setUnique_key_date_column(String unique_key_date_column) {
		this.unique_key_date_column = unique_key_date_column;
	}

	public boolean isIf_exists_skip() {
		return if_exists_skip;
	}

	public void setIf_exists_skip(boolean if_exists_skip) {
		this.if_exists_skip = if_exists_skip;
	}

	public String getTable_name() {
		return table_name;
	}

	public String getTableWithAlias(){
		if(this.table_alias!=null){
			return this.table_name+"("+this.table_alias+")";
		}else{
			return this.table_name;
		}
	}

	public void setTable_name(String table_name) {
		this.table_name = table_name;
	}

	public String getTable_alias() {
		if(this.table_alias==null) {
			return table_name;
		}
		return this.table_alias;
	}

	public void setTable_alias(String table_alias) {
		this.table_alias = table_alias;
	}

	public List<DimColumn> getEtl_columns() {
		return etl_columns;
	}

	public void setEtl_columns(List<DimColumn> etl_columns) {
		this.etl_columns = etl_columns;
	}

	public DimColumn findAutoIdColumn(){
		List<DimColumn> etl_columns = this.getEtl_columns();
		for(DimColumn dc:etl_columns){
			if(dc.getIs_auto_id()){
				return dc;
			}
		}
		return null;
	}

	public DimColumn findDimColumnByCol(String col){
		List<DimColumn> etl_columns = this.getEtl_columns();
		for(DimColumn dc:etl_columns){
			if(col.equals(dc.getCol())){
				return dc;
			}
		}
		return null;
	}

	@Override
	public String toString() {
		return "DimTable [table_name=" + getTable_name() +
				", table_alias="+this.getTable_alias()+
				", etl_columns=" + etl_columns +
				", unique_key_columns="+this.getUnique_key_columns()+
				", unique_key_date_column="+this.getUnique_key_date_column()+
				", uncheck_columns="+this.getUncheck_columns()+
				", if_exists_skip="+this.isIf_exists_skip()+
				"]";
	}

	public JSONObject toJson(){
		JSONObject jo=new JSONObject();
		jo.put("table_name",this.table_name);
		if(this.table_alias!=null){
			jo.put("table_alias",this.table_alias);
		}
		if(this.getEtl_columns()!=null && this.getEtl_columns().size()>0){
			JSONArray ja=new JSONArray();
			for(DimColumn dc:this.getEtl_columns()){
				JSONObject joj=dc.toJson();
				ja.add(joj);
			}
			jo.put("etl_columns",ja);
		}
		//unique columns
		JSONObject ukjo=new JSONObject();
		ukjo.put("if_exists_skip",this.if_exists_skip);
		if(this.unique_key_date_column!=null){
			ukjo.put("unique_key_date_column",this.unique_key_date_column);
		}
		if(this.uncheck_columns!=null){
			JSONArray ja=new JSONArray();
			ja.addAll(this.uncheck_columns);
			ukjo.put("uncheck_columns",ja);
		}
		if(this.unique_key_columns!=null){
			JSONArray ja=new JSONArray();
			ja.addAll(this.unique_key_columns);
			ukjo.put("unique_key_columns",ja);
		}
		return jo;
	}

}
